import { invoke } from '@tauri-apps/api/core';

export interface XiaohongshuAccount {
    id: string;
    username: string;
    nickname: string;
    phone: string;
    status: string;
    followers: number;
    following: number;
    posts: number;
    last_login: string;
    cookies?: string;
    token?: string;
    is_current: boolean;
    created_at: string;
    updated_at: string;
}

export interface LoginResult {
    success: boolean;
    message: string;
    account?: XiaohongshuAccount;
    cookies?: string;
    token?: string;
}

export interface SyncResult {
    success: boolean;
    message: string;
    data?: any;
}

export class XiaohongshuService {
    // 打开登录页面
    async openLoginPage(): Promise<{ success: boolean; message: string }> {
        try {
            const result = await invoke('xiaohongshu_open_login_page');
            return result as { success: boolean; message: string };
        } catch (error) {
            console.error('打开登录页面失败:', error);
            return {
                success: false,
                message: `打开登录页面失败: ${error}`
            };
        }
    }

    // 检查登录状态
    async checkLoginStatus(): Promise<{ isLoggedIn: boolean; message: string }> {
        try {
            const result = await invoke('xiaohongshu_check_status');
            return result as { isLoggedIn: boolean; message: string };
        } catch (error) {
            console.error('检查登录状态失败:', error);
            return {
                isLoggedIn: false,
                message: `检查登录状态失败: ${error}`
            };
        }
    }

    // 获取登录信息并保存账号
    async getLoginInfoAndSave(): Promise<LoginResult> {
        try {
            const result = await invoke('xiaohongshu_get_login_info_and_save');
            return result as LoginResult;
        } catch (error) {
            console.error('获取登录信息失败:', error);
            return {
                success: false,
                message: `获取登录信息失败: ${error}`
            };
        }
    }

    // 同步账号数据
    async syncData(accountId: string): Promise<SyncResult> {
        try {
            const result = await invoke('xiaohongshu_sync_data', { accountId });
            return result as SyncResult;
        } catch (error) {
            console.error('同步数据失败:', error);
            return {
                success: false,
                message: `同步数据失败: ${error}`
            };
        }
    }

    // 切换账号
    async switchAccount(accountId: string): Promise<{ success: boolean; message: string }> {
        try {
            const result = await invoke('xiaohongshu_switch_account', { accountId });
            return result as { success: boolean; message: string };
        } catch (error) {
            console.error('切换账号失败:', error);
            return {
                success: false,
                message: `切换账号失败: ${error}`
            };
        }
    }

    // 获取当前账号
    async getCurrentAccount(): Promise<XiaohongshuAccount | null> {
        try {
            const result = await invoke('xiaohongshu_get_current_account');
            return result as XiaohongshuAccount | null;
        } catch (error) {
            console.error('获取当前账号失败:', error);
            return null;
        }
    }

    // 发布文章
    async publish(title: string, content: string, images?: string[]): Promise<{ success: boolean; message: string }> {
        try {
            const result = await invoke('xiaohongshu_publish', { title, content, images });
            return result as { success: boolean; message: string };
        } catch (error) {
            console.error('发布文章失败:', error);
            return {
                success: false,
                message: `发布文章失败: ${error}`
            };
        }
    }
} 